﻿
using DMTroube_ZeKer.Model;

namespace DMTroube_ZeKer.Control
{
	public partial class TabPanel_dm : UserControl
	{
		private List<TabData>? _tools;
		private List<Panel> panels = new List<Panel>();
		private List<Label> labels = new List<Label>();
		private int checkIndex = 0;
		public List<TabData>? Tools
		{
			get
			{
				return _tools;
			}
			set
			{
				_tools = value;
				if (_tools?.Count > 0)
				{
					for (int i = 0; i < _tools.Count; i++)
					{
						Panel panel = new Panel();
						System.Windows.Forms.Label label = new System.Windows.Forms.Label();
						panel.Name = _tools[i].Name;
						panel.Dock = DockStyle.Fill;
						if (i == 0)
						{
							panel.Visible = true;
							label.BackColor = ColorTranslator.FromHtml("#27ae60");
						}
						else
						{
							panel.Visible = false;
							label.BackColor = Color.FromArgb(52, 152, 219);
						}
						GroupBox groupBox = new GroupBox();
						groupBox.Dock = DockStyle.Fill;
						groupBox.Text = _tools[i].Name;
						if (_tools[i].Control != null)
							groupBox.Controls.Add(_tools[i].Control);
						panel.Controls.Add(groupBox);
						this.Controls.Add(panel);
						panels.Add(panel);
						label.BorderStyle = BorderStyle.FixedSingle;
						label.ForeColor = Color.FromArgb(236, 240, 241);
						label.Size = new Size(23, 78);
						label.Dock = DockStyle.Top;
						label.TabIndex = 0;
						label.Text = _tools[i].Name;
						label.TextAlign = ContentAlignment.MiddleCenter;
						label.Cursor = Cursors.Hand;
						label.Click += Label_Click;
						label.TabIndex = i;
						flowLayoutPanel1.Controls.Add(label);
						labels.Add(label);
					}
				}
			}
		}

		private void Label_Click(object? sender, EventArgs e)
		{
			this.Invoke(() =>
			{
				Label? label = sender as Label;
				if (label != null)
				{
					label.BackColor = ColorTranslator.FromHtml("#27ae60");
					checkIndex = label.TabIndex;
					foreach (var item in labels)
					{
						if (item.TabIndex != label.TabIndex)
						{
							item.BackColor = Color.FromArgb(52, 152, 219);
						}
					}
				}

				foreach (var item in panels)
				{
					if (item.Name == label?.Text)
					{
						item.Visible = true;
					}
					else
					{
						item.Visible = false;
					}
				}
			});
		}

		public TabPanel_dm()
		{
			InitializeComponent();
		}
	}
}
